package com.lcy.springtest.conf;

import com.lcy.springtest.bean.Blue;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @author lichenyang
 * @create 2021-04-16 4:22 下午
 */
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    /**
     * @param importingClassMetadata: 当前类的注解信息
     * @param registry: BeanDefinition的注册类, 把所有要添加到容器中的bean;
     *                使用 BeanDefinitionRegistry.registerBeanDefinitions方法 手工注册进来
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        boolean red = registry.containsBeanDefinition("com.lcy.springtest.bean.Red");
        boolean yellow = registry.containsBeanDefinition("com.lcy.springtest.bean.Yellow");
        if(red && yellow){
            // 指定 Bean 定义信息; (Bean的类型等信息)
            RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(Blue.class);
            // 指定bean的id
            registry.registerBeanDefinition("blue", rootBeanDefinition);
        }
    }
}
